﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Seagull.DAL;

namespace Seagull.TestGeneric
{
    public class DirectoryHelper
    {
        public static string CreateTempFolder(string postfix = "", string basePath = null)
        {
            return Directory.CreateDirectory(
                Path.Combine(basePath ?? Path.GetTempPath(),
                DateTime.Now.ToString("yyyyMMdd HHmmss fff ") +
                Guid.NewGuid().ToString("N") + " " + postfix)).FullName;
        }

        public static string[] GetSubDirs(string parent)
        {
            return Directory.EnumerateDirectories(parent, "*", SearchOption.AllDirectories)
                .Select(p => p.Replace(parent, String.Empty)).OrderBy(p => p).ToArray();
        }

        public static string[] GetSubFiles(string parent)
        {
            return Directory.EnumerateFiles(parent, "*.*", SearchOption.AllDirectories)
                .Select(p => p.Replace(parent, String.Empty)).OrderBy(p => p).ToArray();
        }

        public static void ExpectDirectoryHasNoSubDirectory(string folder)
        {
            Assert.That(Directory.GetDirectories(folder), Is.Empty);
        }

        public static void ExpectDirectoryHasNoFile(string folder)
        {
            Assert.That(Directory.GetFiles(folder), Is.Empty);
        }

        public static string[] CreateDateDirs(string parentDir, IEnumerable<DateTime> dates)
        {
            var dateDirs = dates.Select(date => parentDir + (date.ToString(PathHelper.DateFormatString))).ToArray();
            foreach (var dateDir in dateDirs)
            {
                Directory.CreateDirectory(dateDir);
            }
            return dateDirs;
        }

        public static bool DeleteIfExist(string path)
        {
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
                return true;
            }
            return false;
        }
    }
}
